package com.sunnada.edu.system.dept.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sunnada.edu.system.dept.bean.Dept;
import com.sunnada.edu.system.dept.service.DeptService;
import com.sunnada.edu.system.pub.common.BasicController;
import com.sunnada.edu.system.pub.common.Constants;
import com.sunnada.edu.system.pub.common.Tree;
import com.sunnada.edu.system.pub.common.TreeMethods;
import com.sunnada.edu.system.pub.util.Utils;
/***
 * explain 用户控制层 
 * @author chs 2015-5-12
 * version 1.0
 */
@Controller
@RequestMapping("/deptController")
public class DeptController extends BasicController{
	@Resource
	DeptService deptService;
	
	/*
	 * 部门树
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/deptTree")
	public void deptTree(HttpServletResponse response,HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Dept.class, params);
		String recId = params.get("recId");
		params.put("schoolId", params.get("loginSchoolId"));
		List list=deptService.deptTree(params);
		List treeList = new ArrayList<Tree>();
		treeList = TreeMethods.setTreePropertyForDept(list,true);
		printInfo(response,JSONArray.toJSONString(treeList).toString());
	}

	/**用户添加页面*/
	@RequestMapping("/deptAdd")
	public String userAdd(HttpServletRequest request) { 
		 
		HashMap<String, String> params = new HashMap<String, String>();
		String userId = (String)request.getSession().getAttribute(Constants.USER_ID_SESSION);
		params.put("userId", userId);
		JSONObject json=new JSONObject();
		json.put("userEmployee", null);
		json.put("userDeveloper", null);
		System.out.println(json.toJSONString());
		request.setAttribute("jsuser",json.toJSONString());
		return "system/dept/deptAdd";
	}
	/*
	 * 部门树1
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("/deptTree1")
	public void deptTree1(HttpServletResponse response,HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Dept.class, params);
		List list=deptService.deptTree1(params);
		JSONObject map = new JSONObject();
		map.put("list", list);
		printInfo(response,map);
	}
	
	/**保存功能 */
	@RequestMapping("/deptSave")
	public void deptSave(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		String schoolId =(String) request.getSession().getAttribute(Constants.USER_SHCHOOLID);
		setReqParams(request, Dept.class, params);
		params.put("schoolId", schoolId);
		String recId = params.get("recId"); 		
		if (Utils.isNull(recId)) {
			msg = deptService.deptAdd(params);
		}else{
			msg = deptService.deptUpdate(params);
		}
		printInfo(response,msg);
	}
	
	/**模块修改页面*/
	@SuppressWarnings("rawtypes")
	@RequestMapping("/deptEdit")
	public String deptEdit(HttpServletRequest request) { 
		String recId=request.getParameter("recId");
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Dept.class, params);
		params.put("recId", recId);
		Map dept = deptService.getDeptByRecId(params);
		request.setAttribute("dept", dept);
		return "system/dept/deptEdit";
	}

	/**删除 */
	@RequestMapping("/deptDel")
	public void deptDel(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Dept.class, params);
		msg = deptService.deptDel(params);
		printInfo(response,msg);
	}
	
	
	/**校验部门名称是否唯一 */
	@RequestMapping("/deptNameCheck")
	public void deptNameCheck(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Dept.class, params);
		msg = deptService.deptNameCheck(params);
		printInfo(response,msg);
	}

	

}
